# !/usr/bin/python3
# -*- coding: utf-8 -*-
# @Time    : 2021/10/9 11:26
# @Author  : zuodengbo
# @Email   : zuodb@amoydx.com
# @File    : page_helper
# @Software: PyCharm
from sanic.log import logger
from utils.const import DefaultConst


def page_helper(request):
    """
    分页公共方法
    :param request:
    :return:
    """
    default_num = DefaultConst.page_num
    default_size = DefaultConst.page_size
    try:
        if request.method == "GET":
            page_num = int(request.args.get("page_num", default_num))
            page_size = int(request.args.get("page_size", default_size))
        else:
            page_num = int(request.json.get("page_num", default_num))
            page_size = int(request.json.get("page_size", default_size))

        if page_num < default_num:
            page_num = default_num
        if page_num < default_num:
            page_size = default_size
    except Exception as e:
        logger.warning("When get offset or limit, failure occurred:{}".format(e))
        page_size = default_num
        page_num = default_size

    limit = page_size
    offset = (page_num - 1) * limit

    return offset, limit
